package com.liesp.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Controller;

/**
 * 
* 项目名称：Liesp<br>
* 类名称：StringUtil <br>  
* 类描述：  字符串常用操作工具类 <br>
* 创建人：许彪   
* 创建时间：2009-8-28 上午11:38:06 <br> 
* 修改人：   
* 修改时间：                  <br>  
* 修改备注：   
* @version V1.0
 */
@Controller
public class StringUtil {
	/**
	 * 判断内容是否需要分割.
	 * 
	 * @param content
	 * @return
	 */
	public static boolean checkContent(String content, int length) {
		if (content == null)
			return false;
		if (content.getBytes().length > length)
			return true;
		else
			return false;
	}

	/**
	 * 将第一个字母转换为大写
	 * 
	 * @param s
	 * @return
	 */
	public static String setFirstCharUpcase(String s) {
		if (s == null || s.length() < 1)
			return s;
		char[] c = s.toCharArray();
		if (c.length > 0) {
			if (c[0] >= 'a' && c[0] <= 'z')
				c[0] = (char) ((short) (c[0]) - 32);
		}
		return String.valueOf(c);
	}

	/**
	 * 取指定字符串的某截内容
	 * 
	 * @param src
	 *            原始字符串
	 * @param beginIndex
	 *            开始位置以英文字符为标准
	 * @param length
	 *            要截取的长度,以英文字符为标准,1个中文字符=2个英文字符
	 * @return
	 */
	public static String getString(String src, int beginIndex, int length) {
		String result = "";
		if (beginIndex < 0)
			beginIndex = 0;
		if (src == null || beginIndex >= src.length())
			return "";
		if (beginIndex == 0 && !checkContent(src, length))
			return src;
		try {
			int maxlength = src.length();
			int realLength = length;
			int count = length;
			char c = '0';
			for (int i = beginIndex; count > 0 && i < maxlength; i++) {
				c = src.charAt(i);
				if (c > 127 || c < 0) {// 判断是否为非ascii字符
					realLength--;
					count = count - 2;
				} else {
					count--;
				}
			}
			int endIndex = beginIndex + realLength;
			if (endIndex > maxlength)
				endIndex = maxlength;
			result = src.substring(beginIndex, endIndex);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 取字符串的前一部分字符,会自动加"..".
	 * 
	 * @param src
	 *            源字符串
	 * @param length
	 *            要截断的长度.
	 * @return String
	 */
	public static String getBeginString(String src, int length) {
		if (src == null)
			return "";
		String result = getString(src, 0, length);
		if (result.length() != src.length()) {
			result = getString(src, 0, length - 2);
			result = result + "..";
		}
		return result;
	}

	/**
	 * 获取8位随机数
	 * 
	 * @deprecated 这个方法不能放在此class中（hellion）
	 * @return
	 */
	public static int getRandom() {
		return (int) (Math.random() * 100000000);
	}

	/**
	 * 去掉字符串前后的空格，如果为空则返回""
	 * 
	 * @param a
	 * @return
	 */
	public static String clear(String a) {
		return nvl(a, "").trim();
	}
	
	/**
	 * 去掉字符中所有空格
	 * @param a
	 * @return
	 */
	public static String clearAll(String a){
		// 去掉字符中所有空格
		return nvl(a, "").replaceAll("\\s*", "");
	}
	/**
	 * 当a字符串为空或者长度为0时，返回b字符串
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static String getDefault(String a, String b) {
		String t = clear(a);
		return t.length() < 1 ? b : t;
	}

	/**
	 * 判断字符串为空或者长度是否等于0
	 * 
	 * @param a
	 * @return
	 */
	public static boolean isEmpty(String a) {
		return "".equals(clear(a));
	}

	/**
	 * 判断参数a是否为空，如果为空则返回b (适用与字符串等所有对象)
	 * 
	 * @param <K>
	 * @param a
	 * @param b
	 * @return
	 */
	public static <K> K nvl(K a, K b) {
		return a == null ? b : a;
	}

	/**
	 * 用split 分隔字符串 str 程序自动去掉空白符号
	 * 
	 * @param str
	 *            原始字符串
	 * @param split
	 *            分隔字符串
	 * @return 字符串数组
	 */
	public static String[] splitString(String str, String split) {
		String[] result = null;
		String sPattern = "[\\s]+";
		if (split != null)
			sPattern = "[\\s" + split + "]+";
		try {
			Pattern p = Pattern.compile(sPattern);
			Matcher m = p.matcher(str);
			String strtmp = m.replaceAll(split);
			p = Pattern.compile(split);
			result = p.split(strtmp);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
		return result;
	}
	
	/**
	 * 计算时间差 时间比较 6个小时
	 * @return
	 * @throws ParseException 
	 */
	public static boolean isDatenum(Date olddate,Date newdate) 
		throws ParseException{
		if(olddate==null || newdate == null)
			return true;
		
	    if(olddate.getTime()+1000*60*60*12>newdate.getTime()){
	    	return false;
		}else{
			return true;
		}
	    
		
	}
	/**辛干
	 * hql语句的拼凑，模糊查询
	 * @return
	 * @throws ParseException 
	 */
	
	public String likehql(String hql, String time,String a,String a1, String b,String b1, String c ,String c1,String d,String d1) {
		if ((null != a && !"".equals(a)) || (null != b && !"".equals(b))
				|| (null != c && !"".equals(c))||(null != d && !"".equals(d))) {
			if(time.equals("0")){
				hql = hql + " where ";
			}else{
			    hql=hql+" and ";
			}
			if (null != a && !"".equals(a)) {
				hql = hql +a1+ " like '%" + a + "%'";
				if (null != b && !"".equals(b)) {
					hql = hql + " and "+b1+" like '%" + b + "%'";
				} else if (null != c && !"".equals(c)) {
					hql = hql+" " + " and "+c1+"  like '%" + c + "%'";
				} else if (null != d && !"".equals(d)) {
					hql =hql+" " + " and "+d1+"  like '%" + d + "%'";
				}
			} else if (null != b && !"".equals(b)) {

				hql = hql +" " +b1+ "  like '%" + b + "%'";
				if (null != c && !"".equals(c)) {
					hql = hql +" " +" and "+c1+" like '%" + c + "%'";
				}else if (null != d && !"".equals(d)) {
					hql =hql+" " + " and "+d1+"  like '%" + d + "%'";
				}
			} else if (null != c && !"".equals(c)) {

				hql = hql +" "+ c1+" like '%" + c + "%'";
				if (null != d && !"".equals(d)) {
					hql =hql+" " + " and "+d1+"  like '%" + d + "%'";
				}
			}else if (null != d && !"".equals(d)) {

				hql =hql+" "+d1+"  like '%" + d + "%'";
			}
		}	
		return hql;

	}
	
	/**
	 * 将字符串转化为utf-8编码格式
	 * @author 徐上京
	 * @param str 转换前的字符串
	 * @return {@link String} 转换后的字符串
	 * @throws UnsupportedEncodingException 
	 */
	public static String tranlaterWordTOEncodeUTF8(String str) throws UnsupportedEncodingException{
		String tranlatedStr = URLEncoder.encode(URLEncoder.encode(str,"utf-8"),"utf-8");
		return tranlatedStr;
	}
	
	/**将字符串转化为utf-8编码格式
	 * @author charles 2011-05-08   23:26
	 * @param str 需要转换的String
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String tranlaterISOtoUTF8(String str) throws UnsupportedEncodingException{
		String string=new String(str.getBytes("ISO-8859-1"), "utf-8");
		return string;
	}

}
